<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="schoolId">
        <el-select v-model="queryParams.schoolId" placeholder="请输入所属校区" clearable
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in schoolIdOptions" :key="index" :label="item.schoolName"
                     :value="item.schoolId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="queryParams.studentPhone"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-unlock" size="mini" @click="bindAdviser()">绑定销售</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="12">
        <div style="margin-bottom: 1.5%">
          <li class="el-icon-user-solid"></li>
          <span style="font-size: 14px"> 需要绑定学员:  </span>
          <span style="padding:1px;background-color: #00afff;font-size: 12px;font-weight: bold;color: white">
            {{'总计'+total+'人'}}
          </span>
        </div>
      </el-col>
    </el-row>
    <el-table stripe border v-loading="loading" :data="stuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="学生编号" align="left" prop="studentNumber">
        <template slot-scope="scope">
          <span style="color: #00afff">
            <i class="el-icon-user-solid"></i>
            <span v-text="scope.row.studentNumber"></span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="学员姓名" align="left" prop="studentName"/>
      <el-table-column label="学员电话" align="left" prop="studentPhone"/>
      <el-table-column label="身份证号" align="left" prop="studentIdNumber"/>
      <el-table-column label="出生日期" align="left" prop="studentBirthday"/>
      <el-table-column label="学生性别" align="left" prop="studentSex">
        <template slot-scope="scope">
          <span>
            <i v-if="scope.row.studentSex == '男'" class="el-icon-male"
               style="color:cornflowerblue;margin-right: 5px"></i>
            <i v-if="scope.row.studentSex == '女'" class="el-icon-female" style="color: red;margin-right: 5px"></i>
            <span v-text="scope.row.studentSex"></span>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination :page-size="queryParams.pageSize" layout="total,prev, pager, next" @current-change="currentChange"
                     :total="total">
      </el-pagination>
    </div>

    <div style="width: 50%">
      <el-dialog title="修改绑定" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="绑定销售">
            <el-select v-model="userId" placeholder="绑定销售">
              <el-option  v-for="(item,index) in userIdOptions" :key="index" :label="item.nickName+'('+item.phonenumber+')'" :value="item.userId"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAdviserByStudentId">确 定</el-button>
        </div>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import {editAdviserByStudentId,getAdviserError, getAdviser, getListSchool} from "../../../api/student/student";
import ScrollPane from "../../../layout/components/TagsView/ScrollPane";

export default {
  name: "remind",
  components: {ScrollPane},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      stuList: [],
      //获取所有校区
      schoolIdOptions: [],
      userIdOptions: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        schoolId: null,
        studentPhone: null,
      },
      // 表单校验
      rules: {},
      dialogFormVisible: false,
      userId:null
    };
  },
  created() {
    this.getList();
    this.school();

  },
  methods: {
    bindAdviser() {
      if(this.ids.length == 0){
        this.msgError("请先选择学生")
      }else{
        this.dialogFormVisible = true
        this.getAdviser()
      }
    },
    /** 查询需要更改绑定学生列表 */
    getList() {
      this.loading = true;
      getAdviserError(this.queryParams).then((res) => {
        this.stuList = res.data
        this.total = res.count
        this.loading = false;
      })
    },
    /*更改课程顾问绑定*/
    editAdviserByStudentId(){
      let ids = {};
      ids.id =this.userId;
      ids.idList = this.ids;
        editAdviserByStudentId(ids).then((res) => {
            if(res.code == -1){
               this.msgError("操作失败")
            }
          this.msgSuccess("操作成功")
      })
      this.dialogFormVisible = false
      this.userId = null
      this.ids = null

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.studentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /*获取所有校区*/
    school() {
      getListSchool().then((res) => {
        this.schoolIdOptions = res.data
      })
    },
    /*分页*/
    currentChange(current){
      this.queryParams.pageNum = current;
      this.getList()
    },
    /*获取课程顾问*/
    getAdviser() {
      getAdviser().then((res) => {
        this.userIdOptions = res.data
      })
    }
  }
};
</script>

